//
//  ShopDetailViewCell.m
//  quxiangtao
//
//  Created by HHuajiang on 2018/12/18.
//  Copyright © 2018 shanhaijin. All rights reserved.
//

#import "ShopDetailViewCell.h"

@interface ShopDetailViewCell ()
@property (nonatomic, strong) SDCycleScrollView *shoppingScrollView;
@property (nonatomic, weak) UILabel *currentPriceLabel;
@property (nonatomic, weak) UILabel *oldPriceLabel;
@property (nonatomic, weak) UILabel *titleLabel;
@property (nonatomic, weak) UILabel *discountLabel;
@end

@implementation ShopDetailViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        [self.contentView addSubview:self.shoppingScrollView];
        
        UIView *bgView = [[UIView alloc] init];
        bgView.backgroundColor = WhiteColor;
        [self.contentView addSubview:bgView];
        [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.mas_equalTo(0);
            make.top.equalTo(self.shoppingScrollView.mas_bottom);
        }];
        
        UILabel *currentPriceLabel = [[UILabel alloc] init];
        currentPriceLabel.textColor = Colorff0000;
        [bgView addSubview:currentPriceLabel];
        [currentPriceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(Margin);
            make.top.equalTo(self.shoppingScrollView.mas_bottom).offset(Margin*2);
        }];
        
        UILabel *oldPriceLabel = [[UILabel alloc] init];
        oldPriceLabel.textColor = Color999999;
        oldPriceLabel.font = Font(30);
        [bgView addSubview:oldPriceLabel];
        [oldPriceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(currentPriceLabel.mas_right).offset(Margin);
            make.centerY.equalTo(currentPriceLabel.mas_centerY);
        }];
        
        UILabel *titleLabel = [[UILabel alloc] init];
        titleLabel.textColor = Color333333;
        titleLabel.numberOfLines = 2;
        titleLabel.font = Font(30);
        [bgView addSubview:titleLabel];
        [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(Margin);
            make.top.equalTo(oldPriceLabel.mas_bottom).offset(Margin);
            make.right.mas_equalTo(-jisuan(50));
        }];
        
        UIButton *discountButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [discountButton setBackgroundImage:[UIImage imageNamed:@"SALE"] forState:UIControlStateNormal];
        [bgView addSubview:discountButton];
        [discountButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(Margin);
            make.top.equalTo(titleLabel.mas_bottom).offset(Margin);
            make.right.mas_equalTo(-Margin);
            make.height.mas_equalTo(jisuan(120));
        }];
        [discountButton addTarget:self action:@selector(discountButtonClick) forControlEvents:UIControlEventTouchUpInside];
        
        UILabel *lijilabel = [[UILabel alloc] init];
        lijilabel.text = @"立即领取";
        lijilabel.textColor = WhiteColor;
        lijilabel.font = [UIFont boldSystemFontOfSize:15];
        [discountButton addSubview:lijilabel];
        [lijilabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(-jisuan(45));
            make.centerY.equalTo(discountButton.mas_centerY);
        }];
        
        UILabel *discountLabel = [[UILabel alloc] init];
        discountLabel.text = @"￥5优惠券";
        discountLabel.textColor = WhiteColor;
        [discountButton addSubview:discountLabel];
        [discountLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(jisuan(30));
            make.centerY.equalTo(discountButton.mas_centerY);
        }];
        
        self.titleLabel = titleLabel;
        self.currentPriceLabel = currentPriceLabel;
        self.oldPriceLabel = oldPriceLabel;
        self.discountLabel = discountLabel;
        
    }
    return self;
}

- (void)discountButtonClick {
    if (self.getButtonClickBlock) {
        self.getButtonClickBlock();
    }
}

- (void)setModel:(TbGoodsListModel *)model {
    _model = model;
    
    NSString *oldPrice = [NSString stringWithFormat:@"￥%@", model.goods_price];
    NSDictionary *attributeDic = @{NSStrikethroughStyleAttributeName:[NSNumber numberWithInteger:NSUnderlineStyleSingle]};
    NSMutableAttributedString *oldPirceAttributeStr = [[NSMutableAttributedString alloc] initWithString:oldPrice attributes:attributeDic];
    self.oldPriceLabel.attributedText = oldPirceAttributeStr;
    
    NSString *price = [NSString stringWithFormat:@"￥%@",model.zk_final_price];
    NSMutableAttributedString *priceAttributeStr = [[NSMutableAttributedString alloc] initWithString:price];
    [priceAttributeStr addAttribute:NSFontAttributeName value:Font(30) range:NSMakeRange(0, 1)];
    [priceAttributeStr addAttribute:NSFontAttributeName value:Font(50) range:NSMakeRange(1, price.length - 1)];
    self.currentPriceLabel.attributedText = priceAttributeStr;
    
    NSString *discount = [NSString stringWithFormat:@"￥%@优惠券",model.coupon_amount];
    NSMutableAttributedString *discountAttributeStr = [[NSMutableAttributedString alloc] initWithString:discount];
    [discountAttributeStr addAttribute:NSFontAttributeName value:Font(50) range:NSMakeRange(1, model.coupon_amount.length)];
    self.discountLabel.attributedText = discountAttributeStr;
    
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:model.title];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:5];
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [model.title length])];
    self.titleLabel.attributedText = attributedString;
    [self.titleLabel sizeToFit];
}

- (void)setImageArray:(NSArray *)imageArray {
    _imageArray = imageArray;
    self.shoppingScrollView.imageURLStringsGroup = imageArray;
}

- (SDCycleScrollView *)shoppingScrollView {
    
    if (!_shoppingScrollView) {
        _shoppingScrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, WIDTH, WIDTH) delegate:nil placeholderImage:[UIImage imageNamed:@"supportContentGoodsBG"]];
        _shoppingScrollView.backgroundColor = WhiteColor;
        _shoppingScrollView.autoScroll = NO;
        _shoppingScrollView.showPageControl = YES;
        _shoppingScrollView.infiniteLoop = NO;
    }
    return _shoppingScrollView;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
}

@end
